import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_crop/image_crop.dart';

class EditAvatarController extends GetxController {
  EditAvatarController();

  _initData() {
    update(["edit_avatar"]);
  }

  final cropKey = GlobalKey<CropState>();

  Rx<File?> imageFile = Rx<File?>(null);
 
  @override
  void onReady() {
    var map = Get.arguments;
    if (map != null) {
      var file = map['file'];
      if (file != null && file is String && file.isNotEmpty) {
        imageFile.value = File(file);
      }
    }
    super.onReady();
    _initData();
  }
 
  void clickSave() async {
    // final scale = cropKey.currentState?.scale;

    final area = cropKey.currentState?.area;
    if (area == null) {
      // cannot crop, widget is not setup
      Get.back();
      return;
    }
    final permissionsGranted = await ImageCrop.requestPermissions();
    if (permissionsGranted) {
      final result = await ImageCrop.cropImage(
          file: imageFile.value!,
          area: area,
        );
      Get.back(result: result);
      return;
    }
    Get.back();
  }
}
